﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using WebChatApi.Common.Interface;

namespace WebChatApi.Common
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [KnownType(typeof(ServiceAccount))]    
    public abstract class ThirdPartyServiceAccount<T> : ServiceAccount, IIdentifiable
    {
        #region Properties

        /// <summary>
        /// Gets or sets the host URL.
        /// </summary>
        /// <value>
        /// The host URL.
        /// </value>
        public Uri HostUrl
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the service object.
        /// </summary>
        /// <value>
        /// The service object.
        /// </value>
        public T ServiceObject
        {
            get;
            protected set;
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ThirdPartyServiceAccount{T}"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="uri">The URI.</param>
        /// <param name="serviceIdentity">The service identity.</param>
        /// <param name="SignInIdentity">The SignIn identity.</param>
        /// <param name="SignInPassword">The SignIn password.</param>
        /// <param name="tokenInfo">The token info.</param>
        public ThirdPartyServiceAccount(Guid key, string uri = null, string serviceIdentity = null, string SignInIdentity = null, string SignInPassword = null, string tokenInfo = null)
            : base(key, serviceIdentity, SignInIdentity, SignInPassword, tokenInfo)
        {
            if (!string.IsNullOrWhiteSpace(uri))
            {
                this.HostUrl = new Uri("http://" + uri);
            }

            InitializeServiceObject();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ThirdPartyServiceAccount{T}" /> class.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="serviceIdentity">The service identity.</param>
        /// <param name="SignInIdentity">The SignIn identity.</param>
        /// <param name="SignInPassword">The SignIn password.</param>
        /// <param name="tokenInfo">The token info.</param>
        public ThirdPartyServiceAccount(string uri, string serviceIdentity, string SignInIdentity, string SignInPassword, string tokenInfo)
            : this(Guid.NewGuid(), serviceIdentity, SignInIdentity, SignInPassword, tokenInfo)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ThirdPartyServiceAccount{T}"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="accountInfo">The account info.</param>
        /// <param name="serviceUri">The service URI.</param>
        public ThirdPartyServiceAccount(Guid key, ServiceAccount accountInfo, string serviceUri)
            : this(key, serviceUri, accountInfo.ServiceIdentity, accountInfo.LoginIdentity, null, null)
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ThirdPartyServiceAccount{T}"/> class.
        /// </summary>
        /// <param name="accountInfo">The account info.</param>
        /// <param name="serviceUri">The service URI.</param>
        public ThirdPartyServiceAccount(ServiceAccount accountInfo, string serviceUri)
            : this(Guid.NewGuid(), accountInfo, serviceUri)
        {
        }

        #endregion

        /// <summary>
        /// Initializes the service object.
        /// </summary>
        protected abstract void InitializeServiceObject();
    }
}
